package com.sc.nft.controller;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.enums.mallcart.CartExpiredEnum;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.feign.WxMallCartClient;
import com.sc.nft.pool.UserPool;
import com.sc.nft.req.mallCart.CartEditReq;
import com.sc.nft.req.mallCart.CartOneReq;
import com.sc.nft.req.mallCart.MiniMallCartPageReq;
import com.sc.nft.req.mallCart.RemoveCartReq;
import com.sc.nft.service.MallActivityService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import com.sc.nft.vo.mallcart.MiniMallCartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 小程序购物车
 *
 * @Author：LiGen
 * @Package：com.sc.nft.controller
 * @Project：sc_metago
 * @name：WxMiniMallCartController
 * @Date：2024/5/17 17:56
 * @Filename：WxMiniMallCartController
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/ipMall/wxMiniMall/cart")
@Api(tags = "小程序商城购物车")
public class WxMiniMallCartController {

    private final WxMallCartClient wxMallCartClient;
    private final MallActivityService mallActivityService;

    /**
     * 操作购物车
     *
     * @param req
     * @return
     */
    @ApiOperation(value = "操作购物车", notes = "操作购物车", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result editCart(@Valid @RequestBody CartEditReq req) throws GlobalException {
        req.setMiniUserId(UserPool.getWxMiniUserId());
        Integer num = wxMallCartClient.num(CartOneReq.build(req.getMiniUserId(), req.getActivityId(), req.getActivitySpecsId()));
        CartExpiredEnum cartExpiredEnum = mallActivityService.checkCartExpired(req.getActivityId(), req.getActivitySpecsId(), UserPool.getWxMiniUser(), num + req.getCartNumIncrement());
        if (cartExpiredEnum != CartExpiredEnum.NORMAL) {
            return Result.fail(cartExpiredEnum.getDescription());
        }
        return wxMallCartClient.editCart(req);
    }

    /**
     * 小程序购物车分页
     *
     * @param req
     * @return
     */
    @ApiOperation(value = "小程序购物车分页", notes = "小程序购物车分页", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<MiniMallCartVO> editCart(@Valid @RequestBody MiniMallCartPageReq req) throws GlobalException {
        req.setMiniUserId(UserPool.getWxMiniUserId());
        Page<MiniMallCartVO> page = wxMallCartClient.page(req);
        for (MiniMallCartVO record : page.getRecords()) {
            CartExpiredEnum cartExpiredEnum = mallActivityService.checkCartExpired(record.getActivityId(), record.getActivitySpecsId(), UserPool.getWxMiniUser(), record.getActivitySpecsNum());
            record.setCartExpired(cartExpiredEnum);
        }
        return PageResult.success(page);
    }

    /**
     * 删除购物车
     *
     * @param req
     * @return
     */
    @ApiOperation(value = "删除购物车", notes = "删除购物车", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result deleteCart(@Valid @RequestBody RemoveCartReq req) throws GlobalException {
        req.check();
        req.setMiniUserId(UserPool.getWxMiniUserId());
        return wxMallCartClient.deleteCart(req);
    }

}
